package com.back.controller;

import com.back.common.utils.Result;
import com.back.common.utils.SecurityUtil;
import com.back.dto.InvitationTemplateDTO;
import com.back.entity.InterviewInvitationTemplate;
import com.back.service.ICompanyService;
import com.back.service.IInterviewInvitationTemplateService;
import com.back.vo.InvitationTemplateVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  面试邀请模板前端控制器
 * </p>
 *
 * @author liyang
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/company/{companyId}/invitation-template")
public class InterviewInvitationTemplateController {

    @Autowired
    private IInterviewInvitationTemplateService templateService;

    @Autowired
    private ICompanyService companyService;

    /**
     * 获取模板列表
     */
    @GetMapping("/list")
    public Result getTemplateList(
            @PathVariable Long companyId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        Page<InterviewInvitationTemplate> page = new Page<>(current, size);
        IPage<InvitationTemplateVO> templateList = templateService.getTemplateList(page, companyId);
        return Result.ok().data("templateList", templateList);
    }

    /**
     * 获取模板详情
     */
    @GetMapping("/{id}")
    public Result getTemplateDetail(@PathVariable Long companyId, @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        InvitationTemplateVO template = templateService.getTemplateDetail(id, companyId);
        return Result.ok().data("template", template);
    }

    /**
     * 获取默认模板
     */
    @GetMapping("/default")
    public Result getDefaultTemplate(@PathVariable Long companyId) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        InvitationTemplateVO template = templateService.getDefaultTemplate(companyId);
        return Result.ok().data("template", template);
    }

    /**
     * 创建模板
     */
    @PostMapping
    public Result createTemplate(@PathVariable Long companyId, @RequestBody @Valid InvitationTemplateDTO templateDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        templateDTO.setCompanyId(companyId);
        InvitationTemplateVO template = templateService.createTemplate(templateDTO, userId);
        return Result.ok().data("template", template);
    }

    /**
     * 更新模板
     */
    @PutMapping("/{id}")
    public Result updateTemplate(
            @PathVariable Long companyId,
            @PathVariable Long id,
            @RequestBody @Valid InvitationTemplateDTO templateDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        InvitationTemplateVO template = templateService.updateTemplate(id, companyId, templateDTO);
        return Result.ok().data("template", template);
    }

    /**
     * 删除模板
     */
    @DeleteMapping("/{id}")
    public Result deleteTemplate(@PathVariable Long companyId, @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        boolean success = templateService.deleteTemplate(id, companyId);
        return success ? Result.ok() : Result.error().message("删除失败");
    }

    /**
     * 设置默认模板
     */
    @PostMapping("/{id}/default")
    public Result setDefaultTemplate(@PathVariable Long companyId, @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (!companyService.checkCompanyPermission(companyId, userId)) {
            return Result.error().message("没有操作权限");
        }
        boolean success = templateService.setDefaultTemplate(id, companyId);
        return success ? Result.ok() : Result.error().message("设置失败");
    }
}
